using UnityEngine;
using System.Collections;

public class Popup_Store_SlotSpin2 : Popup_Base {
	public UILabel charNameLabel;
	public UISprite[] traitIconList;
	public UILabel[] traitNameList;
	public GameObject[] unfixMarkObject;
	
	public GameObject[] spinObject;
	public SlotMachineEffectTest[] spinEffect;

    public Transform PCCharacterPosition;
    public Transform NPCCharacterPosition;

    public GameObject[] rareStar;
	
    // Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	int _charId, _additionalTraitId1, _additionalTraitId2 = 0;
	public void SetPopupInfos(int charId, int addotionalTrait1Id, int addotionalTrait2Id)
	{
		_charId = charId;
		_additionalTraitId1 = addotionalTrait1Id;
		_additionalTraitId2 = addotionalTrait2Id;
		DStoreCharacter charInfo = GameData.Inst.GetCharDrawItemInfo(charId);
		
		charNameLabel.text = string.Format("Lv{0}.{1}", charInfo.lv.ToString(), TextController.GetStringWithId(string.Format("charName{0}", charInfo.nameID)));

        string modelName = GameData.Inst.ModelDefineDic[charInfo.modelNo];
        GameObject m_3DModel = BattleResManager.CreatePawn(0, charInfo.modelNo, false);
        m_3DModel.transform.parent = PCCharacterPosition;
        for (int i = 0; i < NPCCharacterPosition.childCount; i++)
        {
            Transform trans = NPCCharacterPosition.GetChild(i);
            if (modelName.ToLower().Contains(trans.gameObject.name))
            {
                m_3DModel.transform.parent = trans;
            }
        }
        m_3DModel.transform.localPosition = Vector3.zero;
        m_3DModel.transform.localRotation = Quaternion.Euler(Vector3.zero);
        m_3DModel.transform.localScale = Vector3.one;
        SceneController.SetAllChildsLayer(m_3DModel.transform, m_3DModel.transform.parent.gameObject.layer);

        var ani = m_3DModel.MakeComponent<Animation>();
        ani["lobby01"].clip.wrapMode = WrapMode.Loop;
        ani.Play("lobby01");

		traitIconList[0].spriteName = charInfo.trait1Value.ToString();
		traitIconList[1].spriteName = charInfo.trait2Value.ToString();
		traitIconList[2].spriteName = charInfo.trait3Value.ToString();
		traitNameList[0].text = TextController.GetStringWithId(string.Format("trait{0}", charInfo.trait1Value));
		traitNameList[1].text = TextController.GetStringWithId(string.Format("trait{0}", charInfo.trait2Value));
		traitNameList[2].text = TextController.GetStringWithId(string.Format("trait{0}", charInfo.trait3Value));
		
		int addotionalTrait1Index = 0; 
		if( addotionalTrait1Id != 0 )
		{
			int[] randomTraitList = GameData.Inst.GetRandomTraitList(charInfo.trait4Value);
			if( !GameData.Inst.IsExistInRandomTraitList(charInfo.trait4Value, addotionalTrait1Id) )
			   randomTraitList = GameData.Inst.GetRandomTraitList(charInfo.trait5Value);
			  
			for(int i=0; i<randomTraitList.Length*4; i++)
			{
                GameObject obj = Resources.Load("Prefab/Store/SpinMain_text") as GameObject;
                UILabel lbl = (Instantiate(obj) as GameObject).GetComponent<UILabel>();
                if (randomTraitList[i % randomTraitList.Length] == 0)
                    lbl.text = TextController.GetStringWithId("uiStoreUndecided");
                else
                    lbl.text = TextController.GetStringWithId(string.Format("trait{0}", randomTraitList[i % randomTraitList.Length]));
                lbl.gameObject.name = string.Format("TraitText{0}", randomTraitList[i % randomTraitList.Length]);
                if (addotionalTrait1Id == randomTraitList[i % randomTraitList.Length])
                    addotionalTrait1Index = i;
                lbl.transform.parent = spinEffect[0].transform;
                lbl.transform.localScale = Vector3.one;
                lbl.transform.localPosition = Vector3.zero;
			}

			if( addotionalTrait1Index != 0 )
			{
				spinObject[0].SetActive(true);
				spinEffect[0].GetComponent<UIGrid>().Reposition();
				unfixMarkObject[0].SetActive(false);
				spinEffect[0].manager = this;
				spinEffect[0].StartSpin(3, addotionalTrait1Index, randomTraitList);
			}
		}
        else
        {
            int[] randomTraitList = GameData.Inst.GetRandomTraitList(charInfo.trait4Value);
            for (int i = 0; i < randomTraitList.Length * 4; i++)
            {
                GameObject obj = Resources.Load("Prefab/Store/SpinMain_text") as GameObject;
                UILabel lbl = (Instantiate(obj) as GameObject).GetComponent<UILabel>();
                if (randomTraitList[i % randomTraitList.Length] == 0)
                    lbl.text = TextController.GetStringWithId("uiStoreUndecided");
                else
                {
                    lbl.text = TextController.GetStringWithId(string.Format("trait{0}", randomTraitList[i % randomTraitList.Length]));
                }
                lbl.gameObject.name = string.Format("TraitText{0}", randomTraitList[i % randomTraitList.Length]);
                if (addotionalTrait1Id == randomTraitList[i % randomTraitList.Length])
                    addotionalTrait1Index = i;
                lbl.transform.parent = spinEffect[0].transform;
                lbl.transform.localScale = Vector3.one;
                lbl.transform.localPosition = Vector3.zero;
            }

            spinObject[0].SetActive(true);
            spinEffect[0].GetComponent<UIGrid>().Reposition();
            unfixMarkObject[0].SetActive(false);
            spinEffect[0].manager = this;
            spinEffect[0].StartSpin(3, randomTraitList.Length * 3, randomTraitList);
        }

		int addotionalTrait2Index = 0; 
		if( addotionalTrait2Id != 0 )
		{
			int[] randomTraitList = GameData.Inst.GetRandomTraitList(charInfo.trait5Value);
			for(int i=0; i<randomTraitList.Length*4; i++)
			{
                GameObject obj = Resources.Load("Prefab/Store/SpinMain_text") as GameObject;
                UILabel lbl = (Instantiate(obj) as GameObject).GetComponent<UILabel>();
                if (randomTraitList[i % randomTraitList.Length] == 0)
                    lbl.text = TextController.GetStringWithId("uiStoreUndecided");
                else
                    lbl.text = TextController.GetStringWithId(string.Format("trait{0}", randomTraitList[i % randomTraitList.Length]));
                lbl.gameObject.name = string.Format("TraitText{0}", randomTraitList[i % randomTraitList.Length]);
                if (addotionalTrait2Id == randomTraitList[i % randomTraitList.Length])
                    addotionalTrait2Index = i;
                lbl.transform.parent = spinEffect[1].transform;
                lbl.transform.localScale = Vector3.one;
                lbl.transform.localPosition = Vector3.zero;
			}

			if( addotionalTrait2Index != 0 )
			{
				spinObject[1].SetActive(true);
				spinEffect[1].GetComponent<UIGrid>().Reposition();
				unfixMarkObject[1].SetActive(false);
				spinEffect[1].manager = this;
				spinEffect[1].StartSpin(4, addotionalTrait2Index, randomTraitList);
			}
		}
        else
        {
            int[] randomTraitList = GameData.Inst.GetRandomTraitList(charInfo.trait5Value);

            for (int i = 0; i < randomTraitList.Length * 4; i++)
            {
                GameObject obj = Resources.Load("Prefab/Store/SpinMain_text") as GameObject;
                UILabel lbl = (Instantiate(obj) as GameObject).GetComponent<UILabel>();
                if (randomTraitList[i % randomTraitList.Length] == 0)
                    lbl.text = TextController.GetStringWithId("uiStoreUndecided");
                else
                    lbl.text = TextController.GetStringWithId(string.Format("trait{0}", randomTraitList[i % randomTraitList.Length]));
                lbl.gameObject.name = string.Format("TraitText{0}", randomTraitList[i % randomTraitList.Length]);
                if (addotionalTrait2Id == randomTraitList[i % randomTraitList.Length])
                    addotionalTrait2Index = i;
                lbl.transform.parent = spinEffect[1].transform;
                lbl.transform.localScale = Vector3.one;
                lbl.transform.localPosition = Vector3.zero;
            }
            
            spinObject[1].SetActive(true);
            spinEffect[1].GetComponent<UIGrid>().Reposition();
            unfixMarkObject[1].SetActive(false);
            spinEffect[1].manager = this;
            spinEffect[1].StartSpin(4, randomTraitList.Length * 3, randomTraitList);
        }

        if (addotionalTrait1Index == 0 && addotionalTrait2Index == 0)
        {
            Invoke("ClosePopup", 3f);
            return;
        }

        StartCoroutine("WaitForEndOfSpin");
	}

    IEnumerator WaitForEndOfSpin()
    {
        while(!spinEffect[0].isDone && !spinEffect[1].isDone)
        {
            yield return null;
        }

        //if (_additionalTraitId1 != 0)
        //    rareStar[3].SetActive(true);
        //if (_additionalTraitId2 != 0)
        //    rareStar[4].SetActive(true);

        yield return new WaitForSeconds(1f);

        ClosePopup();
    }

	public void ClosePopup()
	{
		PopupManager.PopPopup();
        SoundController.PlaySound("UI_card_result");
		Popup_Store_Get popup = PopupManager.PushPopup(PopupKind.StoreSpinResult) as Popup_Store_Get;
		popup.SetInfos(_charId, _additionalTraitId1, _additionalTraitId2);
	}
}
